<?php require_once('common/mysql.php');?>
<?php  
if ($_SERVER['REQUEST_METHOD'] === 'POST') {  
    $username = $_POST['username'] ?? '';  
    $password = $_POST['pwd'] ?? '';  
  
    // 验证用户名和密码  
    $sqlObj = new Sql();  
    $username = mysqli_real_escape_string($sqlObj->link, $username);  
    $password = mysqli_real_escape_string($sqlObj->link, $password);  
    $sql = "SELECT * FROM user WHERE username = '$username' AND password = '$password'";  
    $user = $sqlObj->get_row($sql);  
    if ($user) {  
        // 登录成功，设置cookie  
        setcookie('user', $username, time() + (86400 * 30), "/"); // 有效期30天  
        header('Location: my.php'); // 跳转到my.php页面  
        exit;  
    } else {  
        // 登录失败，显示错误信息  
        echo "用户名或密码错误！";  
    }  
  
    $sqlObj->close(); // 关闭数据库连接  
}  
  
?>  
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>Hadi.记账登录</title>
		<link rel="stylesheet" href="css/login.css" />
		<link rel="stylesheet" href="css/bootstrap.min.css" />
	</head>
	<body>
		<div class="container d-flex justify-content-center align-items-center loginDiv">
			<div class="row justify-content-center loginform">
				<div class="col">
					<div class="row justify-content-center loginTitle">
						Hadi.记账登录
					</div>
					<form action="" method="post">
						<div class="row inputDiv justify-content-center">
							<label class="labcss">用户名：</label>
							<input type="text" placeholder="输入你的用户名" name="username" class="username" />
						</div>
						<div class="row inputDiv justify-content-center">
							<label class="labcss">密码：</label>
							<input type="password" placeholder="输入你的密码" name="pwd" class="pwd" />
						</div>
						<div class="row inputDiv justify-content-center rowBtn">
							<input type="submit" value="登录" class="loginbtn"/>
						</div>
					</form>
					<div class="row text-center rowBtn">
						<div class="col">
							<a href="reg.php" class="rega">没有账号？点此注册</a>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>